import Vue from 'vue'
import ElementUI from 'element-ui'
import VueRouter from 'vue-router'
import moment from 'moment'
import store from './vuex/store'
import 'element-ui/lib/theme-default/index.css'
import './assets/styles/style.css'
import App from './App.vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
import routes from './routes'

Vue.filter('moment', function (value, formatString) {
    formatString = formatString || 'YYYY-MM-DD HH:mm';
    return moment(value).format(formatString);
});

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(VueAxios, axios)

const router = new VueRouter({
    routes
});
router.beforeEach((to, from, next) => {
    //NProgress.start();
    if (to.path == '/login') {
        sessionStorage.removeItem('user');
        next();
    }else{
        console.log(store.state.auth);
        if (!store.state.auth.token){
            next({path:'/login'});
        }else {
            if(!store.state.auth.user){
                store.dispatch("getUserInfo");
            }
            next();
        }
    }

});



new Vue({
  el: '#app',
  render: h => h(App),
    router,
    store,
});
